/*
 * Copyright (c) 2024 iSOFT INFRASTRUCTURE SOFTWARE CO., LTD.
 * easyAda is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */

#ifndef ARM_CPSR_H
#define ARM_CPSR_H

#include <arch/scr.h>
#include <stdlib/stdint.h>

#define MODE_USER       0x0
#define MODE_FIQ        0x1
#define MODE_IRQ        0x2
#define MODE_Supervisor 0x3
#define MODE_Monitor    0x6
#define MODE_Abort      0x7
#define MODE_Hyp        0xA
#define MODE_Undefined  0xB
#define MODE_System     0xF
#define MODE_MASK       0xF

enum CPSR_BITS {
    CPSR_BIT_N = 31,
    CPSR_BIT_Z = 30,
    CPSR_BIT_C = 29,
    CPSR_BIT_V = 28,
    CPSR_BIT_Q = 27,
    CPSR_BIT_J = 24,

    CPSR_BIT_IT_H       = 10,
    CPSR_BIT_IT_H_WIDTH = 6,
    CPSR_BIT_IT_L       = 25,
    CPSR_BIT_IT_L_WIDTH = 2,

    CPSR_BIT_GE       = 16,
    CPSR_BIT_GE_WIDTH = 4,
    CPSR_BIT_E        = 9,
    CPSR_BIT_A        = 8,
    CPSR_BIT_I        = 7,
    CPSR_BIT_F        = 6,

    CPSR_BIT_EE = 25,

    CPSR_BIT_T = 5,
    CPSR_BIT_M = 4,

    CPSR_BIT_MODE       = 0,
    CPSR_BIT_MODE_WIDTH = 4,
};

static inline unsigned long cpsr_nzcv_daif(void)
{
    return scr_read_NZCV() | scr_read_DAIF();
}
static inline int32_t get_current_elx(void)
{
    return ((scr_read_CurrentEL() >> 2) & 0X3);
}

void cpsr_dump_info(void);
#endif
